﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 使用及对比
{
 public   class 双核心处理
    {

        public void Test()
        {
            var ep = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 8888);
            System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
            //端口重用
            socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReuseAddress, true);
            //监听同一端口
            socket.ExclusiveAddressUse = false;
            socket.Bind(ep);
            socket.Listen(20);
            Console.WriteLine("监听端口成功"+ep);
            System.Threading.Tasks.Task.Factory.StartNew(() => {
                byte[] kb2 = new byte[2048];
                while (true) {
                    var abc = socket.Accept();
                    Console.WriteLine("接入{0}", abc.RemoteEndPoint);
                    while (true) {
                        try {
                         int k=   abc.Receive(kb2);
                            if (k == 0) { break; }
                            Console.WriteLine("输出{0}", System.Text.Encoding.GetEncoding("GB2312").GetString(kb2,0,k));
                        }
                        catch (Exception ex) {
                            break;
                        }
                    }
                }
            });


        }
    }
}
